using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace ColladaDotNet.OpenTKMapping
{
	public class SceneImageFile : SceneImage
	{
		private readonly Document document;

		private int glText;

		public SceneImageFile(Document document)
		{
			this.document = document;
		}

		public override void Bind(string texcoord)
		{
			base.Bind(texcoord);

			if (glText == 0)
			{
				Utils.CheckError();

				GL.Enable(EnableCap.Texture2D);
				Utils.CheckError();

				GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
				Utils.CheckError();

				GL.GenTextures(1, out glText);
				Utils.CheckError();
				
				GL.BindTexture(TextureTarget.Texture2D, glText);
				Utils.CheckError();

				Bitmap bitmap;
				var filename = Collada.Item.ToString();
				string fullPath = document.GetFilePath(filename);
				try
				{
					bitmap = (Bitmap)Bitmap.FromFile(fullPath);
				}
				catch(OutOfMemoryException)
				{
					Debug.WriteLine("not enough memory for " + fullPath);
					bitmap = new Bitmap(128, 128, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
					using (var g = Graphics.FromImage(bitmap))
					{
						g.Clear(Color.White);
						g.DrawString("NO MEMORY", new Font(FontFamily.GenericSerif, 14.0f), Brushes.Black, 1, (bitmap.Height - 14) / 2);
						g.DrawRectangle(Pens.Red, 0, 0, bitmap.Width - 1, bitmap.Height - 1);
						g.Flush();
					}
				}
				catch (FileNotFoundException)
				{
					Debug.WriteLine("texture " + fullPath + " not found");
					bitmap = new Bitmap(128, 128, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
					using (var g = Graphics.FromImage(bitmap))
					{
						g.Clear(Color.White);
						g.DrawString("not found",new Font(FontFamily.GenericSerif,14.0f),Brushes.Black,1,(bitmap.Height-14)/2);
						g.DrawRectangle(Pens.Red, 0, 0, bitmap.Width - 1, bitmap.Height - 1);
						g.Flush();
					}
				}
				//bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);

				using (bitmap)
					{
						BitmapData data = bitmap.LockBits(
								new Rectangle(0, 0, bitmap.Width, bitmap.Height),
								ImageLockMode.ReadOnly,
								System.Drawing.Imaging.PixelFormat.Format32bppArgb);

						GL.TexEnv(TextureEnvTarget.TextureEnv,
	TextureEnvParameter.TextureEnvMode,
	(float)TextureEnvMode.Replace);
						GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.LinearMipmapLinear);
						GL.TexParameter(TextureTarget.Texture2D,
						TextureParameterName.TextureMagFilter,
						(float)TextureMagFilter.Linear);
						Utils.CheckError();

						GL.TexImage2D(
								TextureTarget.Texture2D,
								0,
								PixelInternalFormat.Rgba,
								data.Width,
								data.Height,
								0,
								OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
								PixelType.UnsignedByte,
								data.Scan0);
						Utils.CheckError();

//                        Glu.Build2DMipmap(TextureTarget.Texture2D,
//(int)PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height,
//OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte,
//data.Scan0
//);
						Utils.CheckError();

						bitmap.UnlockBits(data);


					}
				
			}
			GL.Enable(EnableCap.Texture2D);
			Utils.CheckError();
			GL.BindTexture(TextureTarget.Texture2D, glText);
			Utils.CheckError();
		}

		public override void Dispose()
		{
			base.Dispose();

			if (glText != 0)
			{
				GL.DeleteTexture(glText);
				glText = 0;
			}
		}
	}
	public class SceneImageBin : SceneImage
	{

	}
	public class SceneImage : DocumentLibraryElement<image>, IDisposable
	{

		internal override void Load(image colladaData)
		{
			base.Load(colladaData);
		}
		virtual public void Bind(string texcoord)
		{
			
		}

		public virtual void Dispose()
		{
			
		}
	}
}